package com.app.business.utils;

import com.app.business.service.ZsProjectUserService;
import com.app.system.jpa.Wrapper;
import com.app.system.permission.model.UserInfo;
import com.app.system.utils.ApplicationContextProvider;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.WebUtils;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * 用户权限控制
 * Created by yby on 2019/2/21 9:35.
 */
public class UserPermissionUtil {
    private static ZsProjectUserService zsProjectUserService;

    public static List<Integer> getZsProjectIdListByUser() {
        UserInfo userInfo = WebUtils.getLoginUserInfo();

        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        zsProjectUserService = (ZsProjectUserService) applicationContext.getBean(ZsProjectUserService.class);

        List<Integer> list = zsProjectUserService.getZsProjectIdByUserInfoId(userInfo.getId());
        //判断一下当前用户是否有分配的项目，如果没有就加入一个0，防止sql 查询条件in 报错
        if (CollectionUtils.isEmpty(list)) {
            list.add(0); //0可以防止和别的项目id重复
        }
        return list;
    }

    /**
     * 直接将用户权限信息封装到wrapper里面
     * @param wrapper
     */
    public static void zsProjectUserLimit(Wrapper wrapper) {
        if (wrapper != null && !wrapper.getQuery().contains("zsp.id = :"))  //当有查询征收项目id时，不在手动
            wrapper.andIn("zsp.id", UserPermissionUtil.getZsProjectIdListByUser());
    }
}
